package site.lanmushan.framework.mapper.params;

import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author Administrator
 */
public class SlashExpressionValue extends ASTNodeAccessImpl implements Expression {
    Expression expression;
    private String value = "";
    private String prefix = null;
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public SlashExpressionValue(Object value) {
        if (value instanceof Integer) {
            this.expression = new LongValue(((Integer) value).intValue());
        } else if (value instanceof Long) {
            this.expression = new LongValue(((Long) value).longValue());
        } else if (value instanceof String) {
            this.expression = new StringValue((String) value);
        } else if (value instanceof Date) {
            this.expression = new StringValue(sdf.format(value));
        } else if (value instanceof LocalDateTime) {
            this.expression = new StringValue(((LocalDateTime) value).format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
        } else if (value instanceof LocalDate) {
            this.expression = new StringValue(((LocalDate) value).format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
        }
    }


    @Override
    public String toString() {
        return expression.toString();
    }

    @Override
    public void accept(ExpressionVisitor expressionVisitor) {
        expressionVisitor.visit((AllComparisonExpression) expression);
    }
}
